import Exif from "exif-js";
export default {
    /**
     * 图片压缩
     * @param obj
     * @param obj.file  图片文件
     * @param obj.size 图片大小 1M  1*1024*1024
     * @param obj.width 宽度
     * @param obj.height 高度
     * @param obj.quality 质量  0.1-1
     */
    compress(obj, callback) {
        obj = Object.assign({
            file: null,
            size: 1 * 1024 * 1024,
            width: 1024,
            height: null,
            quality: 0.8
        }, obj);
        let file = obj.file;
        if (file.size <= obj.size) {
            callback(file);
            return true;
        }

        //获取图片的宽高
        this.getWH(file)
            .then((res) => {
                if (res.w > obj.width) {
                    this.compressWork(res, obj,callback);
                }else{
                    callback(file);
                }
            })
            .catch(error => console.log(error));
    },
    /**
     * 获取图片的宽高
     * @param file
     */
    getWH(file) {
        return new Promise((success) => {
            let fileRead = new FileReader();
            fileRead.readAsDataURL(file);
            fileRead.onload = function () {
                let base64 = this.result;
                let img = new Image();
                img.src = base64;
                img.onload = function () {
                    success({img, w: img.width, h: img.height});
                }
            }
        });
    },

    /**
     * canvas缩放
     */
    compressWork(image, obj,callback) {
        var self = this;
        let canvas = document.createElement('canvas'),
            context = canvas.getContext('2d'),
            scole = image.w / image.h;
        canvas.width = 1024;
        canvas.height = 1024/scole;
        context.drawImage(image.img, 0, 0, image.w, image.h, 0, 0, canvas.width, canvas.height);
        if(navigator.userAgent.match(/iphone/i)){
            Exif.getData(image.img,function(){
                var Orientation = Exif.getTag(this,'Orientation');
                switch (Orientation) {
                    case 6:   //顺时针90度
                        self.rotateImg(this,'left',canvas);
                        break;
                    case 8:  //逆时针90度
                        context.rotate(90);
                        break;
                    case 3:  //180度
                        context.rotate(180);
                        break;
                }
                let src = canvas.toDataURL('image/jpeg', obj.quality);
                let blob = self.dataURLtoBlob(src);
                callback(self.blobToFile(blob,obj.file.name));
            });
        }else{
            let src = canvas.toDataURL('image/jpeg', obj.quality);
            let blob = self.dataURLtoBlob(src);
            callback(self.blobToFile(blob,obj.file.name));
        }
    },

    //将base64转换为blob
    dataURLtoBlob(dataurl){
        var arr = dataurl.split(','),
            mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]),
            n = bstr.length,
            u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new Blob([u8arr], {type: mime});
    },
    //将blob转换为file
    blobToFile(theBlob, fileName) {
        return new File([theBlob],fileName);
    }
}
